/**
 * 
 */
package com.ntv.persistent.wishes.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import com.ntv.domain.IWishes;
import com.ntv.domain.Wishes;
import com.ntv.persistent.AbstractDao;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.util.DateHelper;
import com.ntv.persistent.wishes.IWishesDao;

/**
 * @author vedamoorthi vairam
 * @since 11/01/2009
 * 
 */
public class WishesDao extends AbstractDao implements IWishesDao {
	
	private SimpleJdbcInsert jdbcInsert;
	
	interface CONST {
		interface QUERY {
			String CURRENT_DAY_WISHES = "select id,"  +
					" fullname_who_is," +
					" native_place,"+
					" fullname_whom_to,"+ 			
					" subject," +
					" image," +
					" message,"+ 
					" a.created_by,"+ 
					" a.created_date,"+	 
					" a.publish_date,"+
					" a.is_authorized,"+
					" a.content_id content_id" +					
					" from wishes a left join content b"+
					" on a.content_id =  b.content_id" +
					" where DATE_FORMAT(publish_date,'%Y%m%d') = DATE_FORMAT(now(),'%Y%m%d')" +
					" limit 0, 50";


			String ALL_WISHES_BY_PUBLISH_DATE = "select id,"  +
					" fullname_who_is," +
					" native_place,"+
					" fullname_whom_to,"+ 			
					" subject," +
					" image," +
					" message,"+ 
					" a.created_by,"+ 
					" a.created_date,"+	 
					" a.publish_date,"+
					" a.is_authorized,"+
					" a.content_id content_id" +					
					" from wishes a left join content b"+
					" on a.content_id =  b.content_id" +
					" where DATE_FORMAT(publish_date,'%Y-%m-%d') = :publish_date" + 
					" limit 0, 50";
		}
	}
	
	public List<IWishes> getCurrentDayWishes() throws PersistentException {
		try
		{
			List<IWishes> newsWishes = getNamedParameterJdbcTemplate().query(CONST.QUERY.CURRENT_DAY_WISHES, new HashMap(), getRowMapper());
			return newsWishes;	
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
	
	public List<IWishes> getAllWishesByPublishDate(Date date)  throws PersistentException{
		return null;
	}

	public void saveWish(Wishes wishes) throws PersistentException {
		
		if( wishes != null){
			try {
				jdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(getTableName()).usingGeneratedKeyColumns("id");
				jdbcInsert.usingColumns("fullname_who_is","native_place","fullname_whom_to","subject","message","publish_date", "image");
				
				Map<String, Object> parameters = new HashMap<String, Object>(10);
				parameters.put("fullname_who_is", wishes.getFullnameWhoIs());
				parameters.put("native_place", wishes.getNativePlace());
				parameters.put("fullname_whom_to", wishes.getFullnameWhomTo());
				parameters.put("subject", wishes.getSubject());
				parameters.put("message", wishes.getMessage());
												
				parameters.put("publish_date", DateHelper.getParsedDateString(wishes.getPublishDateString()));
				parameters.put("image", wishes.getImage());
				
				Number id = jdbcInsert.executeAndReturnKey(parameters);
				
			}catch (DataAccessException ex) {
				System.out.println(" Data Access Exception");
				ex.printStackTrace();
				throw new PersistentException(ex);
			}
		}
	}

	public List<IWishes> getWishesByDate(String date) throws PersistentException {
		List<IWishes> newsWishes = null;
		try
		{
			
			 SqlParameterSource namedParameters = new MapSqlParameterSource("publish_date", date);
	     	 newsWishes = getNamedParameterJdbcTemplate().query(CONST.QUERY.ALL_WISHES_BY_PUBLISH_DATE, namedParameters, getRowMapper());
			
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
		return newsWishes;	
	}
}
